package com.dyw.spzx.manager.helper;

import com.dyw.spzx.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {


    // 构建树形结构
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        List<SysMenu> treeMenuList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
             if (sysMenu.getParentId().longValue() == 0) {
                 treeMenuList.add(findChildren(sysMenu,sysMenuList));
             }
        }
        return treeMenuList;
    }

    // 使用递归查找子节点
    public static SysMenu findChildren(SysMenu sysMenu,List<SysMenu> treeNodes){
        sysMenu.setChildren(new ArrayList<SysMenu>());
        for (SysMenu treeNode : treeNodes) {
            if (sysMenu.getId().longValue() == treeNode.getParentId().longValue()) {
                sysMenu.getChildren().add(findChildren(treeNode,treeNodes));
            }
        }
        return sysMenu;

    }


}
